package peace.gui.controls.list{
import flash.display.DisplayObject;
import flash.display.InteractiveObject;
import flash.display.Shape;
import flash.events.MouseEvent;

import peace.data.Constraints;
import peace.data.Dimensions;
import peace.graphic.tween.Motion;
import peace.graphic.tween.Tween;
import peace.graphic.tween.TweenParameter;
import peace.graphic.tween.easing.Easing;
import peace.graphic.tween.easing.RegularEasing;
import peace.gui.controls.RubberSprite;
import peace.gui.layout.Container;
import peace.gui.layout.RowLayout;

public class Carousel extends RubberSprite {
    public var itemSize:Dimensions;
    public const items:Container = new Container(new RowLayout());

    protected var maskShape:Shape = new Shape();
    protected var background:DisplayObject;
    protected var itemsConstrains:Constraints = new Constraints();

    protected var tweenDuration:int = 15;
    protected var tweenSpeed:int = 30;
    protected var tween:Tween;
    protected var dx:TweenParameter = new TweenParameter('x');

    protected var currentIndex:int = 0;
    protected var visibleItemsLength:int = 0;

    public function Carousel(itemSize:Dimensions, spacing:Number = 10) {
        this.itemSize = itemSize;
        items.layout.hSpacing = spacing;
        init();
    }

    private function init():void {
        addChild(items);
        items.mask = maskShape;
        addChild(maskShape);

        tween = new Tween(tweenDuration, true, tweenSpeed);
        var motion:Motion = new Motion(items, tweenDuration, new RegularEasing(Easing.INOUT));
        motion.addParameter(dx);
        tween.addMotion(motion)
    }

    public function setSkin(leftArrow:InteractiveObject, fastLeftArrow:InteractiveObject,
                            rightArrow:InteractiveObject, fastRightArrow:InteractiveObject,
                            itemsContainerConstrains:Constraints = null, background:DisplayObject = null):void {
        leftArrow.addEventListener(MouseEvent.CLICK, onLeft, false, 0, true);
        fastLeftArrow.addEventListener(MouseEvent.CLICK, onFastLeft, false, 0, true);
        rightArrow.addEventListener(MouseEvent.CLICK, onRight, false, 0, true);
        fastRightArrow.addEventListener(MouseEvent.CLICK, onFastRight, false, 0, true);
        if (itemsContainerConstrains) {
            this.itemsConstrains = itemsContainerConstrains;
            items.x = itemsConstrains.left;
            items.y = itemsConstrains.top;
        }
        if (background) {
            this.background = background;
            resize(background.width, background.height)
        }
    }

    private function onFastLeft(e:MouseEvent):void {
        scrollTo(currentIndex - visibleItemsLength)
    }

    private function onLeft(e:MouseEvent):void {
        scrollTo(currentIndex - 1)
    }

    private function onFastRight(e:MouseEvent):void {
        scrollTo(currentIndex + visibleItemsLength)
    }

    private function onRight(e:MouseEvent):void {
        scrollTo(currentIndex + 1)
    }

    protected function scrollTo(index:int):void {
        if (index > items.length - visibleItemsLength) index = items.length - visibleItemsLength;
        if (index < 0) index = 0;
        if (index == currentIndex) return;

        dx.begin = items.x;
        dx.end = itemsConstrains.left - DisplayObject(items.children[index]).x;
        tween.gotoAndPlay(0);
        currentIndex = index
    }

    override public function arrange():void {
        if (widthChanged) {
            var w:Number = width - itemsConstrains.left - itemsConstrains.right;
            with (maskShape.graphics) {
                clear();
                beginFill(0);
                drawRect(itemsConstrains.left, itemsConstrains.top, w, height - itemsConstrains.top - itemsConstrains.bottom)
            }
            items.userWidth = w;
            items.arrange();

            var itemWithSpacing:Number = itemSize.width + items.layout.hSpacing;
            visibleItemsLength = int(w / itemWithSpacing) + int(w % itemWithSpacing / itemSize.width);
        }
        super.arrange();
    }
}
}


